import { Component, OnInit } from '@angular/core';
import { TreeNode } from 'primeng/primeng';
import { FormsModule, Validators, FormGroup, FormControl } from '@angular/forms';
import { DictionaryControlService } from './dictionary-control.service';
import { AppPaginationComponent, PaginationModel } from "../../../widgets/pagination/pagination.component";
import { environment } from '../../../../environments/environment';
import * as _ from 'lodash';
@Component({
  selector: 'app-dictionary-control',
  templateUrl: './dictionary-control.component.html',
  styleUrls: ['./dictionary-control.component.scss'],
  providers: [DictionaryControlService]
})
export class DictionaryControlComponent implements OnInit {

  public ifEdit = 0;
  public listCount = 10;
  public nDictNo = '';
  public system: any = {
    name: "",
    describe: ""
  };
  /**
   * 字典分页列表数据
   */
  public dictionaryList = [];
  /**
   * 字典列表管理分页
   */
  public dictionaryListPageInfo: PaginationModel = {
    currentPageNum: 1,
    pageSize: 10,
    totalPages: 1,
    total: 0,
    pagesShow: 5,
    startRow: 0,
    endRow: 0,
    pageList: [5, 10, 25, 50, 100]
  };
  /**
   * 查询字典分页列表post数据
   */
  public getDictionaryPost = {
    pageIndex: 1,
    pageSize: environment.pageSize,
    sItemValue: '',
    sItemCode: '',
    sItemDec: '',
    sItemKey:''
  };
  /**
   * 编辑添加字典post数据
   */
  public editOrAddDictionaryPost = {

    sItemCode: "",
    sSortNo: "",
    sItemKey: "",
    sItemValue: "",
    sRemark: "",
    sEditFlag: "1",
    sValidFlag: "1",
    sItemDec: ""

  };
  constructor(public dictionaryControlService: DictionaryControlService) { }

  ngOnInit() {
    this.getDictionaryVal();
  }
  //字典列表管理分页
  public dictionaryPageNavigation(currentPageNum: number) {
    this.dictionaryListPageInfo.currentPageNum = currentPageNum;
    this.getDictionaryVal(this.dictionaryListPageInfo.currentPageNum,
      this.dictionaryListPageInfo.pageSize)
  }
  /**
* 改变字典列表显示记录数
* @param {pageSize}
*/
  public dictionarypageSizeChange(pageSize: number) {
    if (pageSize !== this.dictionaryListPageInfo.pageSize) {
      this.dictionaryListPageInfo.pageSize = pageSize;
      // this.bondsManpageInfo.currentPageNum = 1;
      this.getDictionaryVal();
    }
  }
  /**
   * 点击打开编辑添加modal
   */
  showAddOreditModal(index, item?) {
    this.ifEdit = index;
    if (index == 2) {
      this.nDictNo = item.nDictNo;
      this.editOrAddDictionaryPost.sItemCode = item.sItemCode;
      this.editOrAddDictionaryPost.sItemKey = item.sItemKey;
      this.editOrAddDictionaryPost.sItemValue = item.sItemValue;
      this.editOrAddDictionaryPost.sRemark = item.sRemark;
      this.editOrAddDictionaryPost.sSortNo = item.sSortNo;
      this.editOrAddDictionaryPost.sItemDec = item.sItemDec;
    } else if (index == 1) {
      this.nDictNo = "";
      this.editOrAddDictionaryPost.sItemCode = "";
      this.editOrAddDictionaryPost.sItemKey = "";
      this.editOrAddDictionaryPost.sItemValue = "";
      this.editOrAddDictionaryPost.sRemark = "";
      this.editOrAddDictionaryPost.sSortNo = "";
      this.editOrAddDictionaryPost.sItemDec = "";
    }

    window['$']("#add-dictionary").modal('show');
  }
  /**
   * 字典值分页列表
   */
  getDictionaryVal(pageNum?, pageSize?) {
    this.getDictionaryPost.sItemValue = _.trim(this.getDictionaryPost.sItemValue);
    this.getDictionaryPost.sItemCode = _.trim(this.getDictionaryPost.sItemCode);
    this.getDictionaryPost.sItemDec = _.trim(this.getDictionaryPost.sItemDec);
    //updaetd by dujh @20180704 新增字段 start
    this.getDictionaryPost.sItemKey = _.trim(this.getDictionaryPost.sItemKey);
     //updaetd by dujh @20180704 end
    this.getDictionaryPost.pageIndex = pageNum ? pageNum : this.dictionaryListPageInfo.currentPageNum;
    this.getDictionaryPost.pageSize = pageSize ? pageSize : this.dictionaryListPageInfo.pageSize;
    this.dictionaryControlService.getDictionaryVal(this.getDictionaryPost).subscribe(data => {
      if (data) {
        this.dictionaryList = data.list;
        this.dictionaryListPageInfo.currentPageNum = data.pageNum;
        this.dictionaryListPageInfo.totalPages = data.pages;
        this.dictionaryListPageInfo.total = data.total;
        this.dictionaryListPageInfo.startRow = data.startRow;
        this.dictionaryListPageInfo.endRow = data.endRow;
      }
    })
  }
  /**
   * 添加字典数据
   */
  addDictionary() {
    let that = this;
    if (this.requireAdd()) {
      window['swal']({
        title: "提示",
        text: "是否确定添加 " + this.editOrAddDictionaryPost.sItemValue + " 字典值?",
        type: "warning",
        showCancelButton: true,
        closeOnConfirm: false,
        confirmButtonText: "确认",
        confirmButtonColor: "#DD6B55",
        cancelButtonText: "取消",
        showLoaderOnConfirm: true,
      }, function (isConfirm) {
        if (isConfirm) {
          that.dictionaryControlService.addDictionary(that.editOrAddDictionaryPost).subscribe(data => {
            if (data) {
              window['$']("#add-dictionary").modal('hide');
              window['swal']("成功", "字典值添加成功！", "success");
              that.getDictionaryVal();
            }
          })
        }
      })
    }
  }
  /**
  * 编辑字典数据
  */
  editDictionary() {
    let that = this;
    this.editOrAddDictionaryPost['nDictNo'] = this.nDictNo;
    if (this.requireAdd()) {
      window['swal']({
        title: "提示",
        text: "是否确定保存字典值数据?",
        type: "warning",
        showCancelButton: true,
        closeOnConfirm: false,
        confirmButtonText: "确认",
        confirmButtonColor: "#DD6B55",
        cancelButtonText: "取消",
        showLoaderOnConfirm: true,
      }, function (isConfirm) {
        if (isConfirm) {
          that.dictionaryControlService.editDictionary(that.editOrAddDictionaryPost).subscribe(data => {
            if (data) {
              window['$']("#add-dictionary").modal('hide');
              window['swal']("成功", "字典值修改成功！", "success");
              that.getDictionaryVal();
            }
          })
        }
      })
    }
  }
  // 新增、编辑必填校验
  requireAdd() {
    let that = this;
    let requiredAddTip = [];
    let sItemCode = _.trim(this.editOrAddDictionaryPost.sItemCode);
    let sItemValue = _.trim(this.editOrAddDictionaryPost.sItemValue);
    let sItemKey = _.trim(this.editOrAddDictionaryPost.sItemKey);
    let sItemDec = _.trim(this.editOrAddDictionaryPost.sItemDec);
    if (sItemCode == "" || sItemCode == null) {
      requiredAddTip.push("字典编码");
    }
    if (sItemValue == "" || sItemValue == null) {
      requiredAddTip.push("字典值");
    }
    if (sItemDec == "" || sItemDec == null) {
      requiredAddTip.push("字典名称");
    }
    if (sItemKey == "" || sItemKey == null) {
      requiredAddTip.push("字典值编码");
    }
    if (requiredAddTip.length > 0) {
      let requiredAddTips = requiredAddTip.join("、");
      window['swal']("提示", requiredAddTips + "不能为空", "warning");
      return false;
    } else {
      return true;
    }
  }
  /**
   * 删除单个字典数据
   */
  delDictionary(item) {
    let that = this;
    window['swal']({
      type: "warning",
      showCancelButton: true,
      cancelButtonText: "取消",
      confirmButtonText: "确认",
      closeOnConfirm: false,
      title: "提示",
      showLoaderOnConfirm: true,
      // text: '<select><option value>--请选择--</option><option value>立项中</option><option value>待报会</option><option value>一次反馈</option><option value>二次反馈</option><option value>待受理</option><option value>已获批</option><option value>募集筹备</option><option value>发行中</option><option value>已成立</option><option value>报备中</option><option value>运作中</option><option value>已清盘</option></select>',
      text: "请确认是否删除选中的 " + item.sItemValue + " 字典",
      html: true
    }, function () {
      that.dictionaryControlService.delDictionary(item.nDictNo).subscribe(data => {
        if (data) {
          window['swal']("成功", "字典值删除成功！", "success");
          that.getDictionaryVal();
        }
      })
    })
  }
  // 点击表格头排序
  orderData(colName) {
    let listDataAsc = _.orderBy(this.dictionaryList, [colName], ["asc"]);
    if (JSON.stringify(this.dictionaryList) == JSON.stringify(listDataAsc)) {
      listDataAsc = _.orderBy(this.dictionaryList, [colName], ["desc"]);
    }
    this.dictionaryList = listDataAsc;
  }
}
